#include<iostream>

using namespace std;


struct ComplexLinkedNode{
    int value;
    ComplexLinkedNode * next;
    ComplexLinkedNode * pSibiling;
};

ComplexLinkedNode * createNode(int value){
    ComplexLinkedNode * node = new ComplexLinkedNode();
    node -> value = value;
    node -> next = nullptr;
    node ->  pSibiling = nullptr;
    return node;
}

void builComplexLinkedList(ComplexLinkedNode * node, ComplexLinkedNode * next, ComplexLinkedNode * pSibiling){
    node -> next = next;
    node -> pSibiling = pSibiling;
}

void copyComplexLinkedNode(ComplexLinkedNode * head){
    if ( head == nullptr )
        return ;
    ComplexLinkedNode * pHead = head;
    while ( pHead != nullptr ){
        ComplexLinkedNode * node = new ComplexLinkedNode();
        node -> value = pHead -> value;
        node -> pSibiling = nullptr;
        node -> next = pHead -> next;
        pHead -> next = node;
        pHead = pHead -> next -> next;
    }
    // connectCopyoNode(head);
}

void connectCopyoNode( ComplexLinkedNode * head ){
    if ( head == nullptr )
        return ;
    ComplexLinkedNode * pHead = head;
    while ( pHead != nullptr){
        ComplexLinkedNode * copyHeadNode = pHead -> next;
        if (pHead -> pSibiling){
            copyHeadNode -> pSibiling = pHead -> pSibiling -> next;
        }
        pHead = pHead -> next -> next;
    }
}

ComplexLinkedNode * ReconnectNodes(ComplexLinkedNode * head){
    if ( head == nullptr )
        return nullptr;
    int i = 1;
    ComplexLinkedNode *pHead , *root, *copyNode , *copyRoot;
    pHead = root = head;
    copyNode = copyRoot = head -> next;
    while ( head != nullptr ){
        if ( i%2 == 0 ){
            copyNode = copyNode -> next = pHead-> next;
        }else {
            pHead = pHead -> next = copyNode->next;
        }
        i++;
        head = head -> next;
    }
    return copyRoot;
}

void printNode (ComplexLinkedNode * head){
    if ( head == nullptr )
        return;
    ComplexLinkedNode * pHead = head;
    while ( pHead != nullptr ){
        cout << pHead -> value << "  ";
        if ( pHead -> pSibiling ){
            cout << pHead -> pSibiling -> value << "  ";
        }
        cout << endl;
        pHead = pHead -> next;
    }
}

int main(void){
    ComplexLinkedNode * head = createNode(1);
    ComplexLinkedNode * node2 = createNode(2);
    ComplexLinkedNode * node3 = createNode(3);
    ComplexLinkedNode * node4 = createNode(4);
    ComplexLinkedNode * node5 = createNode(5);
    ComplexLinkedNode * node6 = createNode(6);

    builComplexLinkedList(head, node2, node3);
    builComplexLinkedList(node2, node3, node4);
    builComplexLinkedList(node3, node4, node5);
    builComplexLinkedList(node4, node5, node6);
    builComplexLinkedList(node5, node6, nullptr);
    builComplexLinkedList(node6, nullptr, nullptr);

    printNode(head);
    cout << "----------" << endl;

    copyComplexLinkedNode(head);
    printNode(head);
    cout << "----------" << endl;

    connectCopyoNode(head);
    printNode(head);
    cout << "----------" << endl;

    ComplexLinkedNode * root = ReconnectNodes(head);
    printNode(root);
    return 0;
}